package com.springboot.nucleic.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.springboot.nucleic.po.*;
import com.springboot.nucleic.response.ErrorResponseData;
import com.springboot.nucleic.response.ResponseData;
import com.springboot.nucleic.response.SuccessResponseData;
import com.springboot.nucleic.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
@RequestMapping(value = "/rubbish")
public class RubbishController {

    // 依赖注入
    @Autowired
    private RubbishService rubbishService;

    @Autowired
    private ReceiptService receiptService;

    @Autowired
    private RubbishOrdersService rubbishOrdersService;

    @Autowired
    private ReportService reportService;

    @Autowired
    private EvaluateService evaluateService;
    /**
     * 居民管理分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findRubbish")
    public String findRubbish(Integer pageIndex, Integer pageSize, String realname, String phone, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map<String, String> mp = new HashMap<>();
        PageInfo<Rubbish> pageList = rubbishService.findPageInfo(pageIndex, pageSize, mp);
        model.addAttribute("pageList", pageList);
        return "RubbishList";
    }

    @RequestMapping("/receiptList")
    public String receiptList(Integer pageIndex, Integer pageSize,Model model, HttpServletRequest request){
        PageInfo<Receipt> pageList = receiptService.listPage(pageIndex, pageSize);
        model.addAttribute("pageList", pageList);
        return "ReceiptList";
    }

    /**
     * 流动人口分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findResident2")
    public String findResident2(Integer pageIndex, Integer pageSize, String realname, String phone, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }

        Map<String, String> mp = new HashMap<>();
        Object ad = session.getAttribute("ad");
        PageInfo<Rubbish> pageList = rubbishService.findPageInfo(pageIndex, pageSize, mp);
        model.addAttribute("pageList", pageList);
        return "ResidentList2";
    }

    @RequestMapping("getUnitPriceByType")
    @ResponseBody
    public ResponseData getUnitPriceByType(String type){
        LambdaQueryWrapper<Rubbish> rubbishWrapper = new LambdaQueryWrapper<>();
        rubbishWrapper.eq(Rubbish::getName,type);
        Rubbish rubbish = rubbishService.getOne(rubbishWrapper);
        return new SuccessResponseData(rubbish==null?0:rubbish.getPrice());
    }

    /**
     * 高危人员分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findResident3")
    public String findResident3(Integer pageIndex, Integer pageSize, String realname, String phone, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map<String, String> mp = new HashMap<>();
        Object ad = session.getAttribute("ad");
        if (ad instanceof Manager) {//如果是社区管理员，则需要进行社区分类
            mp.put("community", ((Manager) ad).getCommunity());
        }
        mp.put("status", "02");
        mp.put("realname", realname);
        mp.put("phone", phone);
        PageInfo<Rubbish> pageList = rubbishService.findPageInfo(pageIndex, pageSize, mp);
        model.addAttribute("pageList", pageList);
        return "ResidentList3";
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/addRubbish", method = RequestMethod.POST)
    @ResponseBody
    public String addResident(@RequestBody Rubbish rubbish) {
        rubbishService.addRubbish(rubbish);
        return "200";
    }


    /**
     * 删除
     */
    @RequestMapping("/deleteRubbish")
    @ResponseBody
    public String deleteResident(int id) {
        rubbishService.removeById(id);
        return "ResidentList";
    }


    /**
     * 修改
     */
    @RequestMapping("/updateRubbish")
    @ResponseBody
    public String updateRubbish(@RequestBody Rubbish rubbish) {
        try {
            rubbishService.updateResident(rubbish);
            return "200";
        } catch (Exception e) {
            e.printStackTrace();
            return "201";
        }
    }


    /**
     * 按照ID查询
     */
    @RequestMapping("/findRubbishById")
    @ResponseBody
    public Rubbish findResidentById(String id, Model model, HttpServletRequest request) {
        Rubbish rubbish = rubbishService.findResidentById(id);
        return rubbish;
    }

    @RequestMapping("/addRubbish")
    public String addRubbish() {
        return "AddRubbish";
    }


    @RequestMapping("buyRubbish")
    public String buyRubbish(){
        return "BuyRubbish";
    }

    @RequestMapping("findRubbishOrders")
    public String findRubbishOrders(Integer pageIndex, Integer pageSize,Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        PageInfo<RubbishOrders> pageList = rubbishOrdersService.findPageInfo(pageIndex, pageSize);
        model.addAttribute("pageList", pageList);
        return "RubbishOrdersList";
    }

    @RequestMapping("deleteRubbishOrder")
    @ResponseBody
    public ResponseData deleteRubbishOrder(Integer id){
        rubbishOrdersService.deleteOrder(id);
        return new SuccessResponseData();
    }

    @RequestMapping("updateRubbishOrder")
    @ResponseBody
    public ResponseData updateRubbishOrder(RubbishOrders orders){
        rubbishOrdersService.updateRubbishOrder(orders);
        return new SuccessResponseData();
    }

    @RequestMapping("rubbishCollect")
    public String rubbishCollect(Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
//        PageInfo<RubbishOrders> pageList = rubbishOrdersService.findPageInfo(pageIndex, pageSize);
        PageInfo<RubbishStation> pageList = new PageInfo<>();
        pageList.setPageIndex(1);
        pageList.setPageSize(10);
        List<RubbishStation> stations = new ArrayList<>();
        stations.add(new RubbishStation("大阳废品站","13892929303","李晓四","河南省安阳市西湖村"));
        stations.add(new RubbishStation("西海废品站","13892929303","张三","河南省安阳市西湖村"));
        stations.add(new RubbishStation("猛牛废品站","13892929303","王海","河南省安阳市西湖村"));
        stations.add(new RubbishStation("小茉莉废品站","13892929303","李晓四","河南省安阳市西湖村"));
        pageList.setList(stations);
        pageList.setTotalCount(stations.size());
        model.addAttribute("pageList", pageList);
        return "RubbishCollectList";
    }


    @RequestMapping("orderRubbish")
    @ResponseBody
    public ResponseData orderRubbish(@RequestBody RubbishOrders rubbishOrders){
        rubbishOrders.setCreatetime(new Date());
        rubbishOrdersService.save(rubbishOrders);
        return new SuccessResponseData();
    }


    @PostMapping("editEvaluate")
    @ResponseBody
    public ResponseData editEvaluate(@RequestBody Evaluate evaluate){
        evaluateService.updateEvaluate(evaluate);
        return new SuccessResponseData();
    }

    @RequestMapping("reportCollector")
    public String reportCollector(){
        return "ReportCollector";
    }

    @RequestMapping("reportRubbishCollector")
    @ResponseBody
    public ResponseData reportRubbishCollector( @RequestBody Report report,HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return new ErrorResponseData(101,"请先登录");
        }
        report.setUserid(((Resident)session.getAttribute("ad")).getId());
        report.setCreatetime(new Date());
        reportService.save(report);
        return new SuccessResponseData();
    }

    @RequestMapping("reportList")
    public String reportList(Integer pageIndex, Integer pageSize,Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        PageInfo<Report> pageList = reportService.findPageInfo(pageIndex, pageSize);
        model.addAttribute("pageList", pageList);
        return "ReportList";
    }



    @RequestMapping("deleteReport")
    @ResponseBody
    public ResponseData deleteReport(int id){
        reportService.removeById(id);
        return new SuccessResponseData();
    }

    @RequestMapping("evaluateList")
    public String evaluateList(Integer pageIndex, Integer pageSize,Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("ad") == null) {
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        PageInfo<Evaluate> pageList = evaluateService.findPageInfo(pageIndex, pageSize);
        model.addAttribute("pageList", pageList);
        return "EvaluateList";
    }

    @RequestMapping("saveEvaluate")
    @ResponseBody
    public ResponseData saveEvaluate(@RequestBody Evaluate evaluate){
        evaluate.setCreatetime(new Date());
        evaluateService.save(evaluate);
        return new SuccessResponseData();
    }

    @RequestMapping("deleteEvaluate")
    @ResponseBody
    public ResponseData deleteEvaluate(int id){
        evaluateService.removeById(id);
        return new SuccessResponseData();
    }

}
